package app.auto.designer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import app.auto.designer.R;
import app.auto.designer.memcache.GlobalCache;
import app.auto.designer.tool.SerialUtil;
import app.auto.designer.tool.StorageUtil;
import app.auto.runner.base.MapViewConf;
import app.auto.runner.base.RRes;
import app.auto.runner.base.SPrefUtil;

public class ModelSelectActivity extends ActivityBase {
    Set<String> editedLayout = new TreeSet<String>();
    int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_layout);
        getView(R.id.ivLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



        setContent();
        ((ListView) findViewById(R.id.left_drawer)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                position = pos;

                startActivityForResult(new Intent(ModelSelectActivity.this, ModelDesignActivity.class).putExtra("layout", parent.getItemAtPosition(pos).toString().replace(GlobalCache.getInstance().getEditLayoutTag(), "").replaceAll(" ", "") + ""), 0);
//                SPrefUtil.putValue(getBaseContext(), SPNAME, "left_drawer", name);

            }
        });
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setContent();
    }

    public void setContent() {
        MapViewConf.with(getBaseContext()).conf(MapViewConf.with(getBaseContext()).enbaleFake(true).source(R.layout.auto_string_item)).source(getListRes(), findViewById(R.id.left_drawer)).toView();
        ((ListView) findViewById(R.id.left_drawer)).setSelection(position);
    }

    public List getListRes() {
        RRes.TypeAttrs clz = RRes.getResType().get("layout");
        List list = new ArrayList<String>(clz.attrMaps.keySet());

        for (String s : GlobalCache.getInstance().getEditedLayouts()) {
            if (list.contains(s + "  [Edited]")) {
                list.remove(s + "  [Edited]");
                list.add(s);
            }
            if (list.contains(s)) {
                list.remove(s);
                list.add(0, s + "  [Edited]");
            }
        }
        return list;

    }
}
